﻿@{
    Layout = null;
}
@model Freshbox2._0.MODEL.presellwares
<!DOCTYPE html>


<html>
<head>
    <link rel="shortcut icon" href="favicon.ico">
    <link rel="icon" type="image/gif" href="~/img/animated_favicon1.gif">
    <meta name="viewport" content="width=device-width" />
    <link href="~/UILibs/easyui/themes/default/easyui.css" rel="stylesheet" />
    <link href="~/UILibs/easyui/themes/icon.css" rel="stylesheet" />
    <link href="~/Css/css.css" rel="stylesheet" />

    <script src="~/Scripts/jquery-1.7.1.min.js"></script>
    <script src="~/UILibs/easyui/jquery.easyui.min.js"></script>
    <script src="~/UILibs/easyui/locale/easyui-lang-zh_CN.js"></script>

    <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
    <script src="~/Scripts/datagrid-detailview.js"></script>
    <script src="~/Scripts/imgUp.js"></script>
    <link href="~/Css/common.css" rel="stylesheet" />
    <link href="~/Css/index.css" rel="stylesheet" />
    <title></title>
    <script type="text/javascript">

        $(function () {

            bindUpLoad();

            $('#pwaresStoreName').combobox({
                valueField: "StoreID", //Value字段 
                textField: "StoreName", //Text字段                 
                url: "/PresellWares/GetStore", //数据源 
            });
            $('#pwaresTypeName').combobox({
                valueField: "TypeID", //Value字段 
                textField: "TypeName", //Text字段                 
                url: "/PresellWares/GetWaresType", //数据源 
            });
            $('#pwaresStandardName').combobox({
                valueField: "StandardID", //Value字段 
                textField: "StandardName", //Text字段   
                panelHeight: 'auto',
                multiple: true,
                dataType: "json",
                url: "/PresellWares/GetWaresStandard", //数据源 
            });
            $("#data").datagrid({
                fit: true,
                title: '商品信息管理列表',
                striped: true,
                rownumbers: true,
                columns:  //pwaresId   pwaresName  pwaresStatus pwaresCreatime pwaresPrice  pwaresDescription  pwaresNum  pwaresStoreName  pwaresTypeName   pwaresStandardName  pwaresPicUrl
                    [[
                        {
                            field: 'ck',
                            checkbox: true
                        }, {
                            field: 'pwaresId',
                            title: '商品编号',
                            width: 50,
                            height: 50,
                            halign: 'center',
                            sortable: true,
                            align: 'center'
                        }, {
                            field: 'pwaresName',
                            title: '商品名称',
                            width: 150,
                            height: 50,
                            halign: 'center',
                            align: 'center',

                        }, {
                            field: 'pwaresPrice',
                            title: '商品价格',
                            width: 100,
                            height: 50,
                            halign: 'center',
                            align: 'center'
                        }, {
                            field: 'pwaresDisCount',
                            title: '优惠后价格',
                            width: 100,
                            height: 50,
                            halign: 'center',
                            align: 'center',

                        }, {
                            field: 'pwaresNum',
                            title: '商品数量',
                            width: 100,
                            height: 50,
                            halign: 'center',
                            sortable: true,
                            align: 'center'
                        }, {
                            field: 'pwaresDescription',
                            title: '商品描述',
                            width: 300,
                            height: 50,
                            halign: 'center',
                            sortable: true,
                            align: 'center'
                        }, {
                            field: 'pwaresCreatime',
                            title: '上架日期',
                            width: 180,
                            height: 50,
                            halign: 'center',
                            align: 'center',

                        }, {
                            field: 'pwaresTypeName',//pwaresStoreName  pwaresTypeName   pwaresStandardName  pwaresPicUrl
                            title: '商品类型',
                            width: 100,
                            height: 50,
                            halign: 'center',
                            align: 'center'
                        }, {
                            field: 'pwaresStoreName',
                            title: '所属店铺',
                            width: 100,
                            height: 50,
                            halign: 'center',
                            sortable: true,
                            align: 'center'
                        }, {
                            field: 'pwaresStandardName',
                            title: '商品规格',
                            width: 200,
                            height: 50,
                            halign: 'center',
                            align: 'center',

                        }, {
                            field: 'pwaresPicUrl',
                            title: '商品图片',
                            width: 120,
                            height: 50,
                            halign: 'center',
                            align: 'center',
                            formatter: function (value, row) {
                                var str = "";
                                if (value != "" || value != null) {
                                    str = "<img style=\"height: 50px;width: 100px;\" src=\"" + value + "\"/>";  //网络图片可以  本地图片不知道
                                    return str;
                                }
                            }

                        }, {
                            field: 'pwaresStatus',
                            title: '商品状态',
                            width: 100,
                            height: 50,
                            halign: 'center',
                            align: 'center'
                        }]],
                url: pageConfig.getAction,
                pagination: true,
                pageSize: 25,
                pageList: [25, 30, 35],
                sortName: 'pwaresId',
                sortOrder: 'asc',
                toolbar: '#tool'
            });


            $("#formdialog").dialog({
                width: 500,
                height: 550,
                closed: true,
                modal: true,
                buttons: '#btns'
            });



        });
        var pageConfig = {
            type: null,
            getAction: '@Url.Action("Get")',
            saveAction: '@Url.Action("Save")',
            delAction: '@Url.Action("ModifyState")',
            modifyAction: '@Url.Action("Modify2")',
            getOneAction: '@Url.Action("GetOnlyOne")',
            upLoad: '@Url.Action("PostImg")',
            imgStr: "",


        };
        ///禁用用户   状态改变
        function del() {

            var rows = $("#data").datagrid("getSelections")
            if (rows.length <= 0) {
                $.messager.alert("下架商品", "没有选中的行", "info");
                return;
            }

            $.messager.confirm("确定操作", "您确定要下架该商品吗？", function (flag) {
                if (flag) {
                    var idsStr = rows[0].pwaresId;
                    $.ajax({
                        url: pageConfig.delAction,
                        data: { id: idsStr },
                        dataType: "json",
                        type: "POST",
                        beforeSend: function () {
                            before();
                        },
                        success: function (data) {
                            success(data);
                        },
                        error: function () {
                            error();
                        },
                        complete: function () {
                            complete();
                        }
                    });
                }
            })
        }

        //弹出框
        function alertFormDialog(type) {
            pageConfig.type = type;
           // $("#formdialog form").form("reset");

            $("#pwaresName").val("");
            $("#pwaresDisCount").val("");
            $("#pwaresDescription").val("");
            $("#pwaresNum").val("");
            $("#pwaresPrice").val("");
            $("#pwaresTypeName").combobox('clear');
            $("#pwaresStoreName").combobox('clear');
            $("#pwaresStandardName").combobox('clear');

            if (type == 1) {
                $("#formdialog").dialog("open");
                $("#formdialog").dialog("setTitle", "添加");
                $("#formdialog form").attr("action", pageConfig.saveAction);
            }
            else {
                var rows = $("#data").datagrid("getSelections");

                if (rows.length != 1) {
                    $.messager.alert("提示", "修改时必须且只能选中一条记录！", "info");
                    return;
                }
                if (rows[0].pstandardStatus == 1) {
                    $.messager.alert("提示", "此信息已经被置为无效！不能进行修改", "info");
                    return;
                }
                else {

                    $("#formdialog").dialog("open");
                    $("#formdialog").dialog("setTitle", "修改");
                   // $("#pwaresStandardName").combobox('clear');
                    $('#adminUsername').attr("disabled", true);//不可编辑
                    $.ajax({
                        async: false,
                        url: pageConfig.getOneAction + "?id=" + rows[0].pwaresId,
                        dataType: "json",
                        type: "POST",
                        success: function (data) {
                            //alert([data.pwaresStandardId]); ///1,2,3
                            $("#formdialog input[name='pwaresId']").val(data.pwaresId);
                            $("#pwaresName").val(data.pwaresName);
                            $("#pwaresDisCount").val(data.pwaresDisCount);
                            $("#pwaresPrice").val(data.pwaresPrice);
                            $("#pwaresDescription").val(data.pwaresDescription);
                            $("#pwaresNum").val(data.pwaresNum);
                            $("#pwaresStoreName").combobox('setText', data.pwaresStoreName);
                            $("#pwaresStoreName").combobox('setValue', data.pwaresStoreID);
                            $("#pwaresTypeName").combobox('setText', data.pwaresTypeName);
                            $("#pwaresTypeName").combobox('setValue', data.pwaresTypeID);      
                           // $("#pwaresStandardName").combobox('setValues', [data.pwaresStandardId]);
                             //$("#pwaresPicUrl").val(data.pwaresPicUrl);
                            //$("#pwaresStandardName").val(data.pwaresStandardName);     不成立    
                            //$("#pwaresStandardName").combobox('setValue', data.pwaresStandardName);
                            
                            // $("#pwaresStandardName").combobox('setText', data.pwaresStandardName); 

                            // $("#pwaresStandardName").combobox('setValues', [data.pwaresStandardId]);  
                           

                            $("#pwaresStandardName").combobox('setText', [data.pwaresStandardName]);  //用数组的形式接收
                            

                        }
                    });
                           
                }
            }   
        }

        function closeDptDialog() {
            $("#formdialog").dialog("close")
        }
        //点击保存按钮 
        function save() {

              @*  // pwaresName    pwaresPrice  pwaresDescription  pwaresNum  pwaresStoreName  pwaresTypeName   pwaresStandardName  pwaresPicUrl*@
            if ($("#pwaresName").val() != null && $("#pwaresName").val() != "") {
                var bool = $("#fm").form("validate");
                if (bool) {
                    var rows = $("#data").datagrid("getSelections");
                    var data;
                    if (pageConfig.type == 1) {
                        url = pageConfig.saveAction;
                        data = {
                            pwaresName: $("#pwaresName").val(),
                            pwaresPrice: $("#pwaresPrice").val(),
                            pwaresDisCount: $("#pwaresDisCount").val(),
                            pwaresDescription: $("#pwaresDescription").val(),
                            pwaresStoreid: $("#pwaresStoreName").val(),
                            pwaresStandardId: $("#pwaresStandardName").val(),
                            pwaresNum: $("#pwaresNum").val(),
                            pwaresTypeId: $("#pwaresTypeName").val(),
                            pwaresPicUrl: pageConfig.imgStr // $("#pwaresPicUrl").val(),
                        };
                    }
                    else {
                        url = pageConfig.modifyAction;
                        data = {
                            //areaCode: $("#areaName").combobox('getValue'),
                            pwaresId: $("#formdialog input[name='pwaresId']").val(),
                            pwaresName: $("#pwaresName").val(),
                            pwaresPrice: $("#pwaresPrice").val(),
                            pwaresDisCount: $("#pwaresDisCount").val(),
                            pwaresDescription: $("#pwaresDescription").val(),
                            pwaresStoreid: $("#pwaresStoreName").val(),
                            pwaresTypeId:$("#pwaresTypeName").combobox('getValue'),//$("#pw aresTypeName").val(),//
                            pwaresStandardId:$("#pwaresStandardName").val(),//$("#pwaresStandardName").val(),  // $("#pwaresStandardName").combobox('getValues'),
                            pwaresNum: $("#pwaresNum").val(),
                            pwaresPicUrl: pageConfig.imgStr //$("#pwaresPicUrl").val(),

                        };
                    }
                    $.ajax({
                        url: url,
                        dataType: "json",
                        data: data,
                        type: "POST",
                        beforeSend: function () {
                            $.messager.progress({ title: "执行中", text: "" });
                        },
                        success: function (data) {
                            $.messager.alert("成功", data.msg, "info");
                        },
                        error: function (data) {
                            $.messager.alert("警告", "系统故障", "error", "")
                        },
                        complete: function () {

                            complete();
                        }
                    });

                }
            }
            else {
                $.messager.alert("提示", "请把信息填写完整", "info");
            }
        }

        function searchData() {
            $("#data").datagrid('load', {
                contactorName: $("#txtName").val()

            });
        }

        function before() {
            $.messager.progress({ title: "执行中", text: "" });
        }

        function success(data) {
            $.messager.alert("成功", data.msg, "info");
        }
        function error() {

            $.messager.alert("警告", "系统故障", "error", "")
        }

        function complete() {
            $("#data").datagrid("load");
            $.messager.progress("close");
            $("#formdialog").dialog("close");
        }

        //绑定异步上传图片
        function bindUpLoad() {
            $("#btnUpLoadFile").click(function () {
                // alert($("#fileMenuIcon").val());
                //$.ajax({
                $("#AddDiglogDiv form").submit({
                    url: pageConfig.upLoad,               //+"?file=" + ("#fileMenuIcon").val(),
                    type: "POST",
                    success: function (data) {
                        //将返回的数据加载到隐藏域   "?id=" + rows[0].pwaresId,
                        $("#IconImg").val(data);
                        $("#ShowImgDiv").html("<img src='" + data + "' style='width:100px; height:80px'/>");

                    }
                });

            });
        }

        function postImg() {

            $('#form1').form('submit', {
                url: "/PresellWares/PostImg",
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (data) {
                    //alert(data);  
                    pageConfig.imgStr = data.substring(1, data.length - 1);
                    // alert(pageConfig.imgStr);
                }
            });
            //  alert(pageConfig.imgStr);
        }

    </script>
</head>
<body class="easyui-layout" style="width: 100%; height: 100%">
    <div data-options="region:'center'">
        <table id="data"></table>
        <div id="tool" style="padding: 10px;">
            <div style="margin-bottom: 10px;">
                <div style="margin-bottom: 10px;">
                    &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp 
                    <a class="easyui-linkbutton" style="width: 150px; height: 50px;" iconcls="icon-add" onclick="alertFormDialog(1)">添加商品信息</a> &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp 
                    <a class="easyui-linkbutton" style="width: 150px; height: 50px;" iconcls="icon-edit" onclick="alertFormDialog(2)">修改商品信息</a> &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp 
                    <a class="easyui-linkbutton" style="width: 150px; height: 50px;" iconcls="icon-remove" onclick="del()">下架商品</a>
                    &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp &nbsp&nbsp&nbsp            
                </div>

            </div>
        </div>
    </div>

    <div id="formdialog">
        @*       @using (Ajax.BeginForm(new AjaxOptions
        {
            OnBegin = "before",
            OnComplete = "complete",
            OnSuccess = "success",
            OnFailure = "error",
            HttpMethod = "Post"
        }))
        {
                         *@
        <form id="form1" method="post" enctype="multipart/form-data">
            <div style="padding-left: 10px">
                作品图片:
		                <div class="z_photo upimg-div clear">

                            <section class="z_file fl">
                                <img src="~/img/a11.png" id="picurl" class="add-img">
                                <input type="file" id="pwaresPicUrl" name="pwaresPicUrl" class="file" style="width: 40px; height: 60px; margin-left: 10px" value="" accept="image/jpg,image/jpeg,image/png,image/bmp" multiple onchange="postImg()" />
                            </section>
                        </div>
                @*  <button id="btnUpload" onclick="postImg()" style="width:30px;height:20px" value="上传" />*@
                <aside class="mask works-mask">
                    <div class="mask-content">
                        <p class="del-p ">您确定要删除图片吗？</p>
                        <p class="check-p"><span class="del-com wsdel-ok">确定</span><span class="wsdel-no">取消</span></p>
                    </div>
                </aside>
            </div>
        </form>


        <div class="dialog-table" >


            @Html.HiddenFor(x => x.pwaresId)

            <table width="100%" border="0" cellspacing="0" cellpadding="0" class="tk_table">
                @*  // pwaresName   pwaresDisCount  pwaresPrice  pwaresDescription  pwaresNum  pwaresStoreName  pwaresTypeName   pwaresStandardName  pwaresPicUrl*@
                <tr style="height: 50px">
                    <td>商品名称：</td>
                    <td class="white-tr">
                        <input class="easyui-validatebox" id="pwaresName" style="height: 30px; width: 200px" data-options="required:true" /></td>
                </tr>
                <tr style="height: 50px">
                    <td>商品价格：</td>
                    <td class="white-tr">
                        <input class="easyui-validatebox" id="pwaresPrice" style="height: 30px; width: 200px" data-options="required:true" /></td>
                </tr>
                <tr style="height: 50px">
                    <td>优惠后价格：</td>
                    <td class="white-tr">
                        <input class="easyui-validatebox" id="pwaresDisCount" style="height: 30px; width: 200px" /></td>
                </tr>
                <tr style="height: 50px">
                    <td>商品数量(选填)：</td>
                    <td class="white-tr">
                        <input class="easyui-validatebox" id="pwaresNum" style="height: 30px; width: 200px" /></td>
                </tr>
                <tr style="height: 60px">
                    <td>商品简介：</td>
                    <td class="white-tr">
                        <input class="easyui-validatebox" id="pwaresDescription" aria-multiline="true" style="height: 50px; width: 200px" data-options="required:true" /></td>
                </tr>
                <tr style="height: 50px">
                    <td>商品类型：</td>
                    <td class="white-tr">
                        <input class="easyui-combobox" id="pwaresTypeName" style="height: 30px; width: 200px" data-options="required:true" /></td>
                </tr>
                <tr style="height: 50px">
                    <td>所属店铺：</td>
                    <td class="white-tr">
                        <input class="easyui-combobox" id="pwaresStoreName" style="height: 30px; width: 200px" data-options="required:true" /></td>
                </tr>
                <tr style="height: 50px">
                    <td>商品规格：</td>
                    <td class="white-tr">
                        <input class="easyui-combobox" id="pwaresStandardName" style="height: 30px; width: 200px" /></td>
                </tr>

            </table>
     
        </div>
    
    </div>

    <div id="btns">
        <div style="margin-bottom: 10px">
            <a class="easyui-linkbutton" iconcls="icon-redo" onclick="closeDptDialog()">取消</a>
            <a class="easyui-linkbutton" iconcls="icon-add" onclick="save()">保存</a>
        </div>
    </div>

</body>
</html>
